package com.tarena.lbs.message.web.service.impl;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.common.attach.enums.BusinessTypeEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.message.web.mapper.MsgMapper;
import com.tarena.lbs.message.web.mapper.TimingMsgTemplateMapper;
import com.tarena.lbs.message.web.mapper.TriggerMsgTemplateMapper;
import com.tarena.lbs.message.web.service.MsgService;
import com.tarena.lbs.pojo.attach.dto.AttachDTO;
import com.tarena.lbs.pojo.attach.query.AttachQuery;
import com.tarena.lbs.pojo.message.dos.Msg;
import com.tarena.lbs.pojo.message.dos.TriggerMsgActionTemplate;
import com.tarena.lbs.pojo.message.param.TriggerMessageQueryParam;
import com.tarena.lbs.pojo.message.query.MessageQuery;
import com.tarena.lbs.pojo.message.vo.MessageVo;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 消息业务层实现类
 */
@Service
@Slf4j
public class MsgServiceImpl implements MsgService {

    @Value("${app.attach.url}")
    private String url;

    @Resource
    MsgMapper msgMapper;

    @Resource
    TimingMsgTemplateMapper timingMsgTemplateMapper;

    @Resource
    TriggerMsgTemplateMapper triggerMsgTemplateMapper;

    @Resource
    AttachApi attachApi;

    /**
     * 查询用户最新消息列表
     *
     * @param messageQuery 查询参数
     * @return 消息列表
     */
    @Override
    public List<MessageVo> selectUserNewMessage(MessageQuery messageQuery) {
        log.info("查询条件:{}",messageQuery);
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            messageQuery.setUserId(loginUser.getId());
        }
        List<Msg> msgList = msgMapper.selectUserNewMessage(messageQuery);
        List<MessageVo> result = new ArrayList<>();
        if (messageQuery.getType() == 0) {
            for (Msg msg : msgList) {
                MessageVo messageVo = new MessageVo();
                BeanUtils.copyProperties(msg, messageVo);
                result.add(messageVo);
            }
        } else {
            for (Msg msg : msgList) {
                //查询图片,修改为批量查询
                MessageVo messageVo = new MessageVo();
                BeanUtils.copyProperties(msg, messageVo);
                AttachQuery attachQuery = new AttachQuery();
                attachQuery.setBusinessType(BusinessTypeEnum.ACTIVITY.getType());
                attachQuery.setBusinessId(msg.getActivityId());
                List<AttachDTO> attachInfo = attachApi.getAttachInfoByParam(attachQuery);
                if (!CollectionUtils.isEmpty(attachInfo)) {
                    messageVo.setActivityPic(url + attachInfo.get(0).getFileUuid());
                }
                result.add(messageVo);
            }
        }
        return result;
    }

    /**
     * 查询所有消息
     *
     * @return 所有消息列表
     */
    @Override
    public List<Msg> selectAll() {
        return msgMapper.selectAll();
    }

    /**
     * 根据定时消息任务ID查询消息列表
     *
     * @param timingMsgTaskId 定时消息任务ID
     * @return 消息列表
     */
    @Override
    public List<Msg> selectByTimingMsgTaskId(Integer timingMsgTaskId) {
        return msgMapper.selectByTimingMsgTaskId(timingMsgTaskId);
    }

    /**
     * 更新消息状态
     *
     * @param msg 消息对象
     * @return 更新结果
     */
    @Override
    public boolean updateMsgStatus(Msg msg){
        int row = msgMapper.updateByPrimaryKeySelective(msg);
        return  row>=1?true:false;
    }

    /**
     * 根据ID删除消息
     *
     * @param id 消息ID
     * @return 删除结果
     */
    @Override
    public boolean deleteById(Long id) {
        int row = msgMapper.deleteByPrimaryKey(id);
        return row>=1?true:false;
    }

    @Override
    public BasePageList<TriggerMsgActionTemplate> getActionAll(TriggerMessageQueryParam param) {
        int pageSize = param.getPageSize();
        int pageNum = param.getPageNo();
        //new PageQuery(pageNum, pageSize);
        List<TriggerMsgActionTemplate> taskList =  msgMapper.getActionAll(param);
        PageInfo<TriggerMsgActionTemplate> pageInfo = new PageInfo<>(taskList);
        return new BasePageList<TriggerMsgActionTemplate>(taskList,Long.valueOf(pageInfo.getTotal()));
    }
}
